using System.Data;
using System.Data.SqlClient;

namespace WinFormConnectToSqlServer
{
    public partial class Form1 : Form
    {
        string connectionString = "Server=192.168.56.10;Database=ZhaoxiEdu;User Id=SA;Password=SQLServer2022@;";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // using (SqlConnection conn = new SqlConnection(connectionString))
            // {
            //     try
            //     {
            //         SqlCommand cmd =
            //             new SqlCommand(
            //                 "SELECT id FROM [ZhaoxiEdu].[dbo].[ScoreInfo]",
            //                 conn);
            //         conn.Open();
            //         using (SqlDataReader reader = cmd.ExecuteReader())
            //         {
            //             this.comboBox1.Items = reader.GetValues();
            //         }
            //     }
            //     catch (Exception ex)
            //     {
            //         Console.WriteLine("Unable to connect to the database.");
            //         Console.WriteLine(ex.Message);
            //     }
            // }

            Console.WriteLine("winform程序加载成功");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                try
                {
                    SqlCommand cmd =
                        new SqlCommand(
                            "SELECT COUNT(*) FROM [ZhaoxiEdu].[dbo].[ScoreInfo]",
                            conn);
                    conn.Open();
                    object executeScalar = cmd.ExecuteScalar();
                    this.textBox1.Text = executeScalar.ToString();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Unable to connect to the database.");
                    Console.WriteLine(ex.Message);
                }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                try
                {
                    SqlCommand cmd =
                        new SqlCommand(
                            "SELECT * FROM [ZhaoxiEdu].[dbo].[ScoreInfo] WHERE id = @id",
                            conn);
                    cmd.Parameters.AddWithValue("@id", this.comboBox1.SelectedItem);
                    conn.Open();
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            this.textBox2.Text = reader.GetValue("name").ToString();
                            this.textBox3.Text = reader.GetValue("course").ToString();
                            this.textBox4.Text = reader.GetValue("score").ToString();
                            // Console.WriteLine($"name: {reader["name"]}, course: {reader["course"]}, score: {reader["score"]}");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Unable to connect to the database.");
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }
}